﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

/// <summary>
/// 显示某个文件的多个版本
/// 需要GET参数：
/// name —— 文件名
/// </summary>
public partial class editionmanager : System.Web.UI.Page
{
    private DataTable EditionFileList = new DataTable();

    /// <summary>
    /// 验证登录、进入小组、GET参数完整
    /// 初始化列表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["userID"] == null)
        {
            Response.Redirect("notLogin.html", true);
            return;
        }
        else if (Request.Cookies["groupID"] == null)
        {
            Response.Redirect("welcome.aspx", true);
            return;
        }
        else if (Request.QueryString["name"] == null)
        {
            Response.Redirect("noGETParameter.html", true);
            return;
        }

        string fname = Request.QueryString["name"];

        EditionFileList.Columns.Add("name");
        EditionFileList.Columns.Add("size");
        EditionFileList.Columns.Add("time");
        string path_edition = "~/filestorage/" + Request.Cookies["groupID"].Value + "_GS_ROOT/EM/" + fname;
        path_edition = Server.MapPath(path_edition);
        if (Directory.Exists(path_edition))
        {
            FileInfo[] arrFile = new DirectoryInfo(path_edition).GetFiles();
            foreach (FileInfo fi in arrFile)
            {
                DataRow row = EditionFileList.NewRow();
                row["name"] = fi.Name;
                row["size"] = fi.Length.ToString();
                row["time"] = fi.LastWriteTime.ToString();
                EditionFileList.Rows.Add(row);
            }
            EditionsGridView.DataSource = EditionFileList;
            EditionsGridView.DataBind();
        }
    }

    /// <summary>
    /// 上传一个版本的文件
    /// 重命名文件，加上上传者用户名（此功能内置于upload函数中）
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void EditionUploadButton_Click(object sender, EventArgs e)
    {
        string FilePath = "";
        try
        {
            if (!string.IsNullOrEmpty(EditionFileUpload.FileName))
            {
                if (EditionFileUpload.PostedFile.ContentLength <= 0)
                {
                    ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('上传图片文件为空，请重新选择文件!')</script>");
                    return;
                }
                if (EditionFileUpload.HasFile)
                {
                    if (EditionFileUpload.PostedFile.ContentLength > 4196304*250)
                    {
                        ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('文件大小超过1G，请选择更小的图片上传!')</script>");
                        return;
                    }
                    else
                    {
                        FilePath = Server.MapPath("~/filestorage/" + Request.Cookies["groupID"].Value + "_GS_ROOT/EM/" + Request.QueryString["name"]);
                    }

                    Tools.upload(this, FilePath, EditionFileUpload, true, Request.Cookies["username"].Value);
                    Response.Redirect("editionmanager.aspx?name=" + Request.QueryString["name"]);
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('所选路径不存在!')</script>");
                    return;
                }
            }
        }
        catch (Exception ex_toobig)
        {
            ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('" + ex_toobig.Message + ex_toobig.StackTrace + "!')</script>");
            return;
        }
    }

    /// <summary>
    /// 删除一个版本的文件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DeleteOneEdition(object sender, GridViewDeleteEventArgs e)
    {
        String filename = EditionsGridView.Rows[e.RowIndex].Cells[0].Text;
        string path = "~/filestorage/" + Request.Cookies["groupID"].Value + "_GS_ROOT/EM/" + Request.QueryString["name"] + "/" + filename;
        path = Server.MapPath(path);
        if (File.Exists(path))
        {
            File.Delete(path);
            if (Tools.DeleteEmptyDirectory(Directory.GetParent(path).FullName))
            {
                Response.Redirect("filestorage.aspx", true);
                return;
            }
            else
            {
                Response.Redirect("editionmanager.aspx?name=" + Request.QueryString["name"], true);
                return;
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "ss", "<script>alert('文件已经不存在了!')</script>");
            return;
        }
        
    }
}